package com.bang.nacos.boot.demo.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: DiscoveryController
 * @Desc: TODO
 * @Auther: <Bang>
 * @Date: 2020/09/25 11:02
 * @Version: 1.0
 * @Modified By:
 */
@RestController
@RequestMapping("discovery")
public class DiscoveryController {

    @NacosInjected
    private NamingService namingService;


    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public List<Instance> get(@RequestParam String serviceName) throws NacosException {
        return namingService.getAllInstances(serviceName);
    }

    @RequestMapping(value = "/registerInstance", method = RequestMethod.GET)
    public String registerInstance(@RequestParam String serviceName,@RequestParam String ip,@RequestParam int port) throws NacosException {
        namingService.registerInstance(serviceName,ip,port);
        return "ok";
    }
}
